<?php
ob_start();
require_once '../sub/init.php';
if(@!in_array('manghe',$navarr))exit('该功能已关闭');
$cook_uid=intval($cook_uid);
$id=1;
$jumpurl = HOST.'/m/manghe.php?id='.$id;
require_once ZEAI.'sub/ZeaiUP.php';

if(is_weixin() && empty($cook_openid)){
	$cook_openid = wx_get_openid();
	setcookie("cook_openid",$cook_openid,time()+31536000,"/",$_ZEAI['CookDomain']);
}
if(empty($cook_openid) && !ifint($cook_uid)){
	if($js==1){
		json_exit(array('flag'=>'nologin','jumpurl'=>$jumpurl));
	}else{
		header("Location: ".HOST."/m/login.php?jumpurl=".$jumpurl);
	}
}
require_once ZEAI.'sub/zeai_up_func.php';
require_once ZEAI.'sub/conn.php';
require_once ZEAI.'cache/udata.php';
$row = $db->ROW(__TBL_MANGHE__,"*","id=".$id." AND flag=1","name");
if ($row){
	$title        = trimhtml(dataIO($row['title'],'out'));
	$content      = dataIO($row['content'],'outadm');
	$path_s       = $row['path_s'];
	$flag         = $row['flag'];
	$jzbmtime     = $row['jzbmtime'];
	$ifadmlist    = $row['ifadmlist'];
	$wxshareT     = dataIO($row['wxshareT'],'wx');
	$wxshareC     = dataIO($row['wxshareC'],'wx');
	$price_in  = $row['price_in'];
	$price_out = $row['price_out'];
	$outnum    = $row['outnum'];
	$outdel    = $row['outdel'];
	$ifidentity= intval($row['ifidentity']);
	$ifphoto   = intval($row['ifphoto']);
	$outtime   = intval($row['outtime']);
	$ifmarquee = intval($row['ifmarquee']);
}else{
	alert('亲，内容跑路了~',HOST);
}
switch ($submitok) {
	case 'ajax_iMarquee':
		if($ifmarquee!=1)exit;
		$rt=$db->query("SELECT sex,weixin FROM ".__TBL_MANGHE_U__." WHERE outnum>0 AND fid=".$id." ORDER BY id DESC LIMIT 20");
		$total = $db->num_rows($rt);
		if ($total>0){
			$c='';
			for($i=1;$i<=$total;$i++) {
				$rows = $db->fetch_array($rt,'name');
				if(!$rows) break;
				$sex    = $rows['sex'];
				$weixin = trimhtml(dataIO($rows['weixin'],'out'));
				if($sex==1){
					$photo_s_url = RES.'photo_s1_hide.png';
					$sex_str = '女朋友';
				}else{
					$photo_s_url = RES.'photo_s2_hide.png';
					$sex_str = '男朋友';
				}
				$c .= '<li><img src="'.$photo_s_url.'"><font>'.hidetext($weixin).' 刚刚取走了'.$sex_str.'</font></li>';
			} 
		}
		exit($c);
	break;
	case 'ajax_chkin':
		chkRZ();
		if(ifint($cook_uid) || !empty($cook_openid)){
			$row = $db->ROW(__TBL_MANGHE_U__,"flag","fid=".$id." AND (uid=".$cook_uid." AND uid<>0   OR    openid='$cook_openid' AND openid<>'')","name");
			if (!$row){
				if($price_in>0){
					json_exit(array('flag'=>'pay'));
				}else{
					json_exit(array('flag'=>1));
				}
			}else{
				$my_flag=$row['flag'];
				if($my_flag==1){
					json_exit(array('flag'=>0,'msg'=>'您已经投放过资料，等待有缘人来取你吧'));
				}elseif($my_flag==2){
					json_exit(array('flag'=>1,'msg'=>'已支付空资料提交并激活为1'));
				}
			}
		}
		json_exit(array('flag'=>1));
	break;
	case 'ajax_add_update':
		chkRZ();
		$sex    = intval($sex);
		$weixin = trimhtml(dataIO($weixin,'in',100));
		$yzah = trimhtml(dataIO($yzah,'in',100));
		$aboutus= trimhtml(dataIO($aboutus,'in',500));
		$cook_uid    = intval($cook_uid);
		$cook_openid = dataIO($cook_openid,'in','40');
		if(empty($sex))json_exit(array('flag'=>0,'msg'=>'请选择性别'));
		if (!preg_match('/^[A-Za-z0-9_]+$/u',$weixin) || str_len($weixin)<5)json_exit(array('flag'=>0,'msg'=>'请输入正确微信或手机号'));//&& !ifmob($weixin) 
		$row = $db->ROW(__TBL_MANGHE_U__,"id,flag","fid=".$id." AND (     uid=".$cook_uid." AND uid<>0    OR     openid='$cook_openid' AND openid<>''     OR    weixin='$weixin'    )","name");
		if ($row){
			$my_id  =$row['id'];
			$my_flag=$row['flag'];
			if($my_flag==1){
				json_exit(array('flag'=>0,'msg'=>'您已经投放过资料，等待有缘人来取你吧'));	
			}elseif($my_flag==2){
				$flag2=($price_out>0)?0:1;
				$db->query("UPDATE ".__TBL_MANGHE_U__." SET flag=1,sex='$sex',weixin='$weixin',yzah='$yzah',aboutus='$aboutus',addtime=".ADDTIME.",flag2=$flag2 WHERE id=".$my_id);
			}
		}else{
			if($price_in==0){
				$flag2=($price_out>0)?0:1;
				$db->query("INSERT INTO ".__TBL_MANGHE_U__."  (sex,weixin,yzah,aboutus,fid,uid,openid,addtime,flag,flag2) VALUES ('$sex','$weixin','$yzah','$aboutus','$id','$cook_uid','$cook_openid',".ADDTIME.",1,$flag2)");
				$my_id= $db->insert_id();
			}else{
				json_exit(array('flag'=>0,'msg'=>'请您先支付 '.$price_in.'元'));
			}
		}
		if(ifint($my_id) && !empty($photo_s)){
			UP_modFn('guest','s',$photo_s);
			$path_b = UP_modFn('guest','tmp_p',$photo_s,'weixin');
			$db->query("UPDATE ".__TBL_MANGHE_U__." SET path_b='$path_b' WHERE id=".$my_id);
		}
		//
		if(!empty($ifadmlist)){
			$rt=$db->query("SELECT openid FROM ".__TBL_ADMIN__." WHERE id in ($ifadmlist) AND flag=1 AND subscribe=1 AND openid<>''");
			$total = $db->num_rows($rt);
			if ($total > 0) {
				for($i=1;$i<=$total;$i++) {
					$rows = $db->fetch_array($rt,'name');
					if(!$rows) break;
					$openid=$rows['openid'];
					if (!empty($openid)){
						$first     = urlencode('盲盒资料投放成功');
						$keyword1  = '盲盒投放';
						$keyword3  = urlencode($_ZEAI['siteName']);
						$uidstr=(ifint($cook_uid))?'　用户ID:'.$cook_uid:'';
						$remark    = '性别：'.udata('sex',$sex).'　微信：'.hidetext($weixin).$uidstr;
						$url = $jumpurl;
						@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$openid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url='.$url);
					}
				}
			}	
		}
		json_exit(array('flag'=>1,'msg'=>'存入成功，请等待有缘人'));
	break;
	case 'ajax_chkout':
		if(ADDTIME<=$outtime && $outtime>0)json_exit(array('flag'=>0,'msg'=>'<center>取出对象资料时间将在<br><font class=S18>《'.YmdHis($outtime,'YmdHis').'》</font><br>正式开启<br></center>'));
		if(ifint($cook_uid) || !empty($cook_openid)){
			$row = $db->ROW(__TBL_MANGHE_U__,"id,outnum,sex,flag,flag2","fid=".$id." AND (uid=".$cook_uid." AND uid<>0   OR    openid='$cook_openid' AND openid<>'')","name");
			if ($row){
				$my_id=$row['id'];
				$my_flag=$row['flag'];
				$my_flag2=$row['flag2'];
				$cook_outnum=$row['outnum'];
				$cook_sex=intval($row['sex']);
				if($cook_outnum>=$outnum && $outnum>0)json_exit(array('flag'=>0,'msg'=>'你很贪心哦，每人最多只能取出'.$outnum.'份资料'));
				if($my_flag!=1)json_exit(array('flag'=>0,'msg'=>'亲，请先存入自己资料，再来取对象~~'));
				if($my_flag2!=1 && $price_out>0)json_exit(array('flag'=>'pay','msg'=>'请您先支付 '.$price_out.'元'));
				//
				$row_ = $db->ROW(__TBL_MANGHE_U__,"id,sex,weixin,yzah,aboutus,path_b","fid=".$id." AND sex<>'$cook_sex' AND (uid<>".$cook_uid." OR openid<>'$cook_openid') ORDER BY RAND() LIMIT 1","name");
				if($row_){
					$did=$row_['id'];
					$sex=$row_['sex'];
					$path_b=$row_['path_b'];
					$weixin =trimhtml(dataIO($row_['weixin'],'out'));
					$yzah =trimhtml(dataIO($row_['yzah'],'out'));
					$aboutus=trimhtml(dataIO($row_['aboutus'],'out'));
					if($outdel==1)$db->query("DELETE FROM ".__TBL_MANGHE_U__." WHERE id='$did'");
					$db->query("UPDATE ".__TBL_MANGHE_U__." SET outnum=outnum+1,flag2=0 WHERE id=".$my_id);
					$sexstr=udata('sex',$sex);
					$path_b = (!empty($path_b))?UP.$path_b:RES.'photo_s'.$sex.'.png?'.$_ZEAI['cache_str'];
					json_exit(array('flag'=>1,'path_b'=>$path_b,'sex'=>$sexstr,'weixin'=>$weixin,'yzah'=>$yzah,'aboutus'=>$aboutus,'msg'=>'取出成功'));
				}else{
					json_exit(array('flag'=>0,'msg'=>'亲，此活动过于火爆，异性都被取光了，先息会儿再来碰碰运气吧'));
				}
			}else{
				json_exit(array('flag'=>0,'msg'=>'亲，请先存入自己资料，再来取对象~~'));
			}
		}
	break;
}
function chkRZ() {
	global $db,$ifidentity,$ifphoto,$jumpurl,$cook_uid;
	if($ifidentity==1 || $ifphoto == 1){
		if(!iflogin())json_exit(array('flag'=>'nologin','jumpurl'=>$jumpurl));
		$row = $db->ROW(__TBL_USER__,"RZ","id=".$cook_uid);
		$RZ= $row[0];$RZarr = explode(',',$RZ);
		if(!in_array('identity',$RZarr) && $ifidentity==1)json_exit(array('flag'=>'nocert','msg'=>'本盲盒活动力求全部真实可信，投放资料的会员必须全部实名认证，为了真实性，请您配合先《实名认证》'));
		if(!in_array('photo',$RZarr) && $ifphoto==1)json_exit(array('flag'=>'nocert','msg'=>'本盲盒活动力求全部真实可信，投放资料的会员必须全部实名认证，为了真实性，请您配合先《真人认证》'));
	}
}
function getphoto() {
	global $cook_openid;
	$server_token = wx_get_access_token();
	$wxuinfo = wx_get_uinfo($server_token,$cook_openid);
	//$path_b  = (!empty($wxuinfo['headimgurl']))?wx_get_uinfo_logo($wxuinfo['headimgurl'],'yk','photo'):'';
	$path_b  = (!empty($wxuinfo['headimgurl']))?wx_get_up('tmp',$wxuinfo['headimgurl'],$cook_uid,$mode='B'):'';
	return $path_b;
}
$db->query("UPDATE ".__TBL_MANGHE__." SET click=click+1 WHERE id=".$id);
$bmnum = $db->COUNT(__TBL_MANGHE_U__,"flag=1 AND fid=".$id);
$nav='manghe';?>
<!doctype html><html><head><meta charset="utf-8">
<title><?php echo $title;?> - <?php echo $_ZEAI['siteName'];?></title>
<?php echo HEADMETA;?>
<meta name="generator" content="Zeai.cn v9" />
<link href="<?php echo RES;?>m/css/m.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo RES;?>m/css/manghe.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo HOST;?>/cache/zeaicolor.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/m.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/ZeaiUP.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>var wH=<?php echo (is_weixin())?24.5:31.5;?>,id=<?php echo $id;?>;browser='<?php echo browser();?>',upMaxMB = <?php echo $_UP['upMaxMB']; ?>,up2='<?php echo $_ZEAI['up2'];?>/';</script>
<?php
if (is_weixin()){
	require_once ZEAI."api/weixin/jssdk.php";?>
	<script src="<?php echo RES; ?>jweixin-1.2.0.js"></script>
	<script>
	wx.config({debug:false,appId: '<?php echo $signPackage["appId"];?>',timestamp: <?php echo $signPackage["timestamp"];?>,nonceStr:'<?php echo $signPackage["nonceStr"];?>',signature: '<?php echo $signPackage["signature"];?>',jsApiList:['chooseImage','uploadImage','hideMenuItems','onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','previewImage']});
	var FX_title = '<?php echo $wxshareT;?>',
	FX_desc  = '<?php echo trimhtml($wxshareC).'【'.$_ZEAI['siteName'].'】';?>',
	FX_link  = '<?php echo $jumpurl; ?>',
	FX_imgurl= '<?php echo UP.smb($path_s,'b'); ?>';
	wx.ready(function () {
		wx.onMenuShareAppMessage({title:FX_title,desc:FX_desc,link:FX_link,imgUrl:FX_imgurl});
		wx.onMenuShareTimeline({title:FX_title,link:FX_link,imgUrl:FX_imgurl});
	});
	</script>
<?php }?>
<style>
.detail_BN{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>}
#backtop a{background-color:#fff;color:<?php echo $_ZEAI['mob_mbcolor'];?>}
.big_normal_pic{background-image:url(<?php echo RES;?>m/img/manghe_out.jpg?1);background-size:cover;background-position:center center;background-repeat:no-repeat}
</style>
</head><body>
<?php
if ($_ZEAI['m_mb']==2){$bncls=' style="border-radius:1.2rem"';$TRnav_btn = 'hide';require_once ZEAI.'m/top2.php';}else{
$mini_title="脱单盲盒";
$mini_class='top_mini4 zeaibg bgcolor';
$mini_L = '<em onClick="zeai.back()"><i class="ico" >&#xeaca;</i></em>';
require_once ZEAI.'m/top_mini.php';}?>
<div class="detail_BN"<?php echo $bncls;?>><img src="<?php echo UP.smb($path_s,'b');?>"></div>
<div class="boxx ulist">
    <dl><dt class="zeaicolor"><i class="ico">&#xe638;</i>本期参与嘉宾</dt><dd><i class="ico zeaicolor">&#xe643; <b><?php echo $bmnum;?>人</b></i>已参与</dd></dl>
    <ul>
        <?php
        if($bmnum>0){
            $rt=$db->query("SELECT uid,sex,path_b FROM ".__TBL_MANGHE_U__." WHERE flag=1 AND fid=".$id." ORDER BY id DESC LIMIT 110");
            $total = $db->num_rows($rt);
            if ($total > 0) {
                for($i=1;$i<=$total;$i++) {
                    $rows = $db->fetch_array($rt,'name');
                    if(!$rows) break;
                    $uid = $rows['uid'];
					$path_b  = $rows['path_b'];
					if(ifint($uid)){
						$row = $db->ROW(__TBL_USER__,"sex,photo_s,photo_f,photo_ifshow,flag AS uflag","id=".$uid);
						if ($row){
							$sex      = $row['sex'];
							$photo_s  = $row['photo_s'];
							$photo_f  = $row['photo_f'];
							$photo_ifshow = $row['photo_ifshow'];
							$uflag = $row['uflag'];
							if(empty($path_b)){
								$photo_s_url=photo_s_out('{"smb":"s","sex":"'.$sex.'","photo_s":"'.$photo_s.'","photo_f":"'.$photo_f.'","photo_ifshow":"'.$photo_ifshow.'","uflag":"'.$uflag.'","outkind":"url"}');
							}else{
								$photo_s_url=UP.$path_b;
							}
						}
					}else{
						$sex      = $rows['sex'];
						$photo_s_url = (!empty($path_b))?UP.$path_b:RES.'photo_s'.$sex.'.png?'.$_ZEAI['cache_str'];
					}
                    echo '<li><img src="'.$photo_s_url.'"></li>';
                }
                echo '<li class="more"><i class="ico">&#xe642;</i></li>';
            }
        }else{
            echo '<div class="nodata"><i class="ico">&#xe659;</i>暂时还没有人报名</div>';
        }
        ?>
    </ul>
	<div class="clear"></div>
</div>
<div class="iMarqueeBox"<?php echo($ifmarquee == 1)?'':' style="display:none"';?>>
    <div id="iMarquee" class="iMarquee">
        <li><img src="<?php echo UP.$_ZEAI['logo'];?>?<?php echo $_ZEAI['cache_str'];?>"><font>脱单盲盒-<?php echo $_ZEAI['siteName'];?></font></li>
    </div>
</div>
<div class="boxx C"><?php echo $content;?></div>
<div class="manghe_btm">
	<a id="btn_in"><i class="ico">&#xe669;</i>存入自己</a>
	<a id="btn_out"><i class="ico">&#xe66d;</i>取出对象</a>
</div>
<form id="in_form" class="in_form">
    <dl><dt>性别</dt><dd>
        <input type="radio" name="sex" id="sex1" class="radioskin" value="1"<?php echo ($cook_sex == 1)?' checked':'';?>><label for="sex1" class="radioskin-label"><i class="i2"></i><b class="ico sex1">&#xe637;</b></label>
        <input type="radio" name="sex" id="sex2" class="radioskin" value="2"<?php echo ($cook_sex == 2)?' checked':'';?>><label for="sex2" class="radioskin-label"><i class="i2"></i><b class="ico sex2">&#xe639;</b></label>
    </dd></dl>
    <dl><dt>微信</dt><dd><input class="input" name="weixin" id="weixin" placeholder="输入本人微信或手机" maxlength="50" autocomplete="off"></dd></dl>
    <input class="input" name="yzah" id="yzah" type="hidden" placeholder="对方加你时验证暗号"  value="我在<?php echo $_ZEAI['siteName'];?>盲盒看到你的资料">
    <dl class="aboutus"><dt>脱单宣言</dt><dd><textarea class="textarea" name="aboutus" id="aboutus" placeholder="简短个人介绍(100字以内)"></textarea></dd></dl>
    
    <dl><dt>照片</dt><dd>
        <li id="photo_sbtn">
            <font class="C999 S12"><i class="ico">&#xe609;</i> 点击上传本人近期生活照</font>
        </li>
        <input type="hidden" name="photo_s" id="photo_s" value="">
    </dd></dl>
    
    <em>先存后取，微信号必须真实，否则将纳入失信人员名单并进行<font class="Cf00">封号处理</font></em>
    <input type="hidden" name="id" value="<?php echo $id;?>">
    <input type="hidden" name="submitok" value="ajax_add_update">
    <button type="button" class="btn size3 zeaibg yuan" onClick="mhsend('in_form')"><i class="ico">&#xe669;</i> 放入盲盒</button>
</form>
<div id="out_form">
	<img id="out_path_b">
    <dl><span>性别：<font id="out_sex"></font></span></dl>
    <dl><span>微信：<font id="out_weixin"></font></span><a class="fz" onClick="zeai.copy(out_weixin.innerHTML),zeai.msg('复制成功');">复制</a></dl>
    <dl>介绍：<font id="out_aboutus"></font></dl>
    <dl>暗号：<font id="out_yzah"></font></dl>
</div>
<?php
$ifpay=false;
$row = $db->ROW(__TBL_MANGHE_U__,"flag","fid=".$id." AND (uid=".$cook_uid." AND uid<>0   OR    openid='$cook_openid' AND openid<>'')","name");
if (!$row && $price_in>0){
	$price = $price_in;
	$ifpay=true;
	$inout = 'in';
}elseif($row && $price_out>0){
	$price = $price_out;
	$ifpay=true;
	$inout = 'out';
}
if($ifpay){
	$jump_url = $jumpurl;
	$money = $price;
	$kind  = 24;
	$cook_money=0;
	echo '<input type="hidden" id="money" value="'.$money.'">';
	echo '<input type="hidden" id="inout" value="'.$inout.'">';
	require_once 'my_pay.php';
}
?>
<script src="<?php echo RES;?>m/js/manghe.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>
ZeaiUP.one({url:'manghe'+zeai.ajxext,submitokBef:'ajax_315_',btnobj:photo_sbtn,end:function(e){
	if(e.flag==1){
		photo_sbtn.html('<img src='+up2+e.dbname+'>');
		photo_s.value=e.dbname;
	}else{
		zeai.msg(e.msg);
	}
}});
</script>
<div id="backtop"><a id="btmTopBtn" onClick="zeai.setScrollTop(0);"><i class="ico">&#xe60c;</i>顶部</a></div>
<?php require_once ZEAI.'m/TRnav.php';?>
<div class="clear"></div>
<div class="blank"></div>
<?php if (is_weixin() && ifint($cook_uid)){wx_endurl('您刚刚浏览的页面【脱单盲盒】',$jumpurl);}?>
</body></html>
<?php ob_end_flush();?>